#!/usr/bin/ruby

#################################
#        MFnat.rb                                                                              
#        description;                                                                          
#            provide nat control functions for MFd.rb                                          
#        version           1.0
#        last modified     06/22/06
#       Last change, updated error reporting
#################################

NAT_VERSION=1.0
def nat_ver
   return "\tNAT library version: #{NAT_VERSION}\n"
end


def nat_stop(zone)
    if active?("/jail/nat")
        if !system("#{$iptables} -t nat -F")
            return  "\tFAILED: flushing nat table\n"
        end
    else
        return "\tNOTICE: Nat deactivated in configuration... skipping\n"
    end
end

def nat_start(zone)
    if !active?("/jail/nat")
        return "\tNOTICE: Nat deactivated or invalid... skipping\n"
    end
    rules=MFreadlines("/jail/nat/rules")
    if rules
        retary=Array.new
        rules.each {|r|
        if !system("#{$iptables} -t nat #{r}")
            retary << "\tFAILED: adding rule #{r} to nat table\n"
        end
        }
        return retary.to_s
    else
        return "\tFAILED: Invalid or empty nat rule table\n"
    end
end

def nat_restart(zone)
    retary=Array.new
    retary << nat_stop(zone)
    retary << nat_start(zone)
    return retary.to_s
end

def mangle_stop(zone)
    if active?("/jail/mangle")
        if !system("#{$iptables} -t mangle -F")
            return  "\tFAILED: flushing mangle table\n"
        end
    else
        return "\tNOTICE: Mangle deactivated in configuration... skipping\n"
    end
end

def mangle_start(zone)
    if !active?("/jail/mangle")
        return "\tNOTICE: Mangle deactivated or invalid... skipping\n"
    end
    rules=MFreadlines("/jail/mangle/rules")
    if rules
        retary=Array.new
        rules.each {|r|
        if !system("#{$iptables} -t mangle #{r}")
            retary << "\tFAILED: adding rule #{r} to mangle table\n"
        end
        }
        return retary.to_s
    else
        return "\tFAILED: Invalid or empty mangle rule table\n"
    end
end

def mangle_restart(zone)
    retary=Array.new
    retary << mangle_stop(zone)
    retary << mangle_start(zone)
    return retary.to_s
end
